package com.wanhe.shoppingcart;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wanhe.base.BaseDialogFragment;
import com.wanhe.base.util.NetUtil;
import com.wanhe.base.util.SystemUtil;

/**
 * Created by XieZaiWei on 2017/6/27.
 * 下单弹窗
 */

public class OrderDialog extends BaseDialogFragment implements View.OnClickListener {

	private EditText		et_job_number;
	private EditText		et_table_num;
	private EditText		et_count;
	private EditText		et_pwd;
	private TextView		tvCancle;
	private TextView		tvConfirm;
	private OrderPresenter	mOrderPresenter;

	public static OrderDialog newInstance() {
		OrderDialog dialog = new OrderDialog();
		return dialog;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mView = inflater.inflate(R.layout.order_dialog, container, false);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		et_job_number = (EditText) findViewById(R.id.et_job_number);
		et_table_num = (EditText) findViewById(R.id.et_table_num);
		et_count = (EditText) findViewById(R.id.et_count);
		et_pwd = (EditText) findViewById(R.id.et_pwd);
		tvCancle = (TextView) findViewById(R.id.btnCancle);
		tvConfirm = (TextView) findViewById(R.id.btnConfrim);

		et_pwd.requestFocus();
		tvCancle.setOnClickListener(this);
		tvConfirm.setOnClickListener(this);
	}

	@Override
	public void onStart() {
		super.onStart();
		Dialog dialog = getDialog();
		if (dialog != null) {
			DisplayMetrics dm = new DisplayMetrics();
			getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
			dialog.getWindow().setLayout((int) (dm.widthPixels * 0.4), ViewGroup.LayoutParams.WRAP_CONTENT);
		}
	}

	public void setPresenter(OrderPresenter orderPresenter) {
		mOrderPresenter = orderPresenter;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btnCancle) {// 取消
			dismiss();
		} else if (id == R.id.btnConfrim) {// 确定，下单
			palceOrder();
		}
	}

	/**
	 * 下单
	 */
	private void palceOrder() {
		if (!NetUtil.hasNetwork(getContext())) {
			return;
		}
		String jobNumber = et_job_number.getText().toString().trim();
		if (null == jobNumber || "".equals(jobNumber)) {
			Toast.makeText(getContext(), "请输入员工号，亲!", Toast.LENGTH_SHORT).show();
			return;
		}

		String tableNum = et_table_num.getText().toString().trim();
		if (null == tableNum || "".equals(tableNum)) {
			Toast.makeText(getContext(), "请输入餐桌号，亲!", Toast.LENGTH_SHORT).show();
			return;
		}

		String count = et_count.getText().toString().trim();
		if (null == count || "".equals(count)) {
			Toast.makeText(getContext(), "请输入用餐人数，亲!", Toast.LENGTH_SHORT).show();
			return;
		}

		String pwd = et_pwd.getText().toString();
		if (null == pwd || "".equals(pwd)) {
			Toast.makeText(getContext(), "请输入密码，亲!", Toast.LENGTH_SHORT).show();
			return;
		}
		 mOrderPresenter.placeOrder(jobNumber, tableNum, count, pwd);
		mOrderPresenter.webSocketPlaceOrder(count, jobNumber, pwd);
		dismiss();

	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		SystemUtil.hideSystemKeyBoard(getContext(), et_pwd);
	}
}
